<?php


namespace App\Services\Algo;

use App\Interf\Calculable;
use TestingItemService;
use App\Models\LogHelper;

class BoneHealthDuringPregnancyTestingAlgo extends BaseTestingAlgo implements Calculable
{
    public $allAnswers;
    private $typeFourExpress;

    public function __construct($tid, $answerStr, $typeFourExpress = null)
    {

        parent::__construct($tid, $answerStr);
        $this->typeFourExpress = $typeFourExpress;
        $this->allAnswers = $this->convertAllAnswers();

    }

    public function calculate()
    {
        return $this->entrance($this->tid, $this->answers);

    }

    /**
     * 将拼装后的答案转换成字符串
     * @return string
     */
    public function resultToStr()
    {
        return $this->arrToStr($this->answers);
    }


    public function getAllAnswer()
    {
        return $this->answers;
    }

    /**
     * @param $tid
     * @param $answers
     * @return float|null
     */
    protected function entrance($tid, $answers)
    {
        $answer17= $this->getDefaultAnswerObjByIndex(17);
        $totalScore =$answer17['weight'];
        return $totalScore;
    }

    protected function getDefaultAnswerObjByIndex($index)
    {
        $obj = [];
        foreach ($this->answers as $answer) {
            if ($index == $answer['index']) {
                $obj = $answer;
                break;
            }
        }
        return $obj;
    }


    protected function convertAllAnswers()
    {

        LogHelper::log('before calculate totalScore=============>');
        LogHelper::log($this->answers);
        $TotalScore = [];
        LogHelper::log('this->typeFourExpress=============>');
        LogHelper::log($this->typeFourExpress);
        if ($this->typeFourExpress) {
            foreach ($this->typeFourExpress as $d) {
                if ($d['index'] == 17) {
                    array_push($TotalScore, $d);
                    break;
                }
            }
            $result = $this->fourTypeArray($TotalScore, $this->answers);
            foreach ($result as $v) {
                array_push($this->answers, $v);
            }
        }
        return $this->answers;


    }





}